package com.factory.rental.controller;

import com.factory.rental.common.PageSupport;
import com.factory.rental.common.Result;
import com.factory.rental.model.Room;
import com.factory.rental.dto.RoomDTO;
import com.factory.rental.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/rooms")
public class RoomController {

    private final RoomService roomService;

    @Autowired
    public RoomController(RoomService roomService) {
        this.roomService = roomService;
    }

    @PostMapping("save")
    public Result<Room> createRoom(@RequestBody RoomDTO roomDTO) {
        Room savedRoom = roomService.saveRoom(roomDTO);
        return Result.success(savedRoom, "房间创建成功");
    }

    @GetMapping("/{id}")
    public Result<Room> getRoomById(@PathVariable Long id) {
        Optional<Room> room = roomService.getRoomById(id);
        if (room.isPresent()) {
            return Result.success(room.get());
        } else {
            return Result.error(404, "未找到指定ID的房间");
        }
    }

    @GetMapping("/number/{roomNumber}")
    public Result<Room> getRoomByNumber(@PathVariable String roomNumber) {
        Optional<Room> room = roomService.getRoomByNumber(roomNumber);
        if (room.isPresent()) {
            return Result.success(room.get());
        } else {
            return Result.error(404, "未找到指定编号的房间");
        }
    }

    @GetMapping("/building/{building}")
    public Result<List<Room>> getRoomsByBuilding(@PathVariable String building) {
        List<Room> rooms = roomService.getRoomsByBuilding(building);
        return Result.success(rooms);
    }

    @GetMapping("/floor/{floor}")
    public Result<List<Room>> getRoomsByFloor(@PathVariable Integer floor) {
        List<Room> rooms = roomService.getRoomsByFloor(floor);
        return Result.success(rooms);
    }

    @GetMapping("/status/{status}")
    public Result<List<Room>> getRoomsByStatus(@PathVariable Room.RoomStatus status) {
        List<Room> rooms = roomService.getRoomsByStatus(status);
        return Result.success(rooms);
    }

    @GetMapping("/layout/{layout}")
    public Result<List<Room>> getRoomsByLayout(@PathVariable String layout) {
        List<Room> rooms = roomService.getRoomsByLayout(layout);
        return Result.success(rooms);
    }

    @GetMapping("pageList")
    public Result<Page<Room>> getAllRooms(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageSupport.ofPage(page, size);
        Page<Room> rooms = roomService.getAllRooms(pageable);
        return Result.success(rooms);
    }

    @PutMapping("/{id}")
    public Result<Room> updateRoom(@PathVariable Long id, @RequestBody RoomDTO roomDTO) {
        try {
            Room updatedRoom = roomService.updateRoom(id, roomDTO);
            return Result.success(updatedRoom, "房间信息更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的房间");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteRoom(@PathVariable Long id) {
        try {
            roomService.deleteRoom(id);
            return Result.success(null, "房间删除成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的房间");
        }
    }

    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateRoomStatus(@PathVariable Long id, @PathVariable Room.RoomStatus status) {
        try {
            roomService.updateRoomStatus(id, status);
            return Result.success(null, "房间状态更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的房间");
        }
    }
}